//
//  UserViewController.m
//  Happy Kitchen
//
//  Created by 冫柒Yun on 15/3/12.
//  Copyright (c) 2015年 mac_chen. All rights reserved.
//

#import "UserViewController.h"

@interface UserViewController ()

@end

@implementation UserViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    
    self.navigationItem.title = @"我的";
    
    
    UIImageView *headImageView = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, self.view.frame.size.width, self.view.frame.size.height / 2 - 80)];
    headImageView.backgroundColor = [UIColor cyanColor];
    [self.view addSubview:headImageView];
    
    // 创建tableView对象
    UITableView *userTableView  = [[UITableView alloc] initWithFrame:CGRectMake(0, headImageView.frame.origin.y + headImageView.frame.size.height + 30, self.view.frame.size.width, self.view.frame.size.height - headImageView.frame.size.height)];
    
    // 设置tableView的分割线颜色
    userTableView.separatorColor = [UIColor clearColor];
    // 设置tableView不滑动
    userTableView.scrollEnabled = NO;
    
    // 指定代理人
    userTableView.delegate = self;
    userTableView.dataSource = self;
    
    [self.view addSubview:userTableView];
    
}

// cell的行数
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return 3;
}
// cell的内容
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString *cellIdetifier = @"cell";
    UITableViewCell *cell = [tableView dequeueReusableHeaderFooterViewWithIdentifier:cellIdetifier];
    if (cell == nil) {
        cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellIdetifier];
    }
    
    if (indexPath.section == 0 && indexPath.row == 0) {
        cell.textLabel.text = @"我的收藏";
    }
    if (indexPath.section == 0 && indexPath.row == 1) {
        cell.textLabel.text = @"清除缓存";
    }
    if (indexPath.section == 0 && indexPath.row == 2) {
        cell.textLabel.text = @"关于我们";
    }
    
    cell.textLabel.textAlignment = 1;
    cell.textLabel.font = [UIFont systemFontOfSize:20];
    
    return cell;
}

// 点击cell触发方法
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    if (indexPath.section == 0 && indexPath.row == 0) {
        MyCollectViewController *myCollectVC = [[MyCollectViewController alloc] init];
        
        // 当点击我的收藏时候, 就打开数据库
        [[ClassifyDataBase sharkClassifyDataBase] openDB];
        [[FoodDataBase shareMenuDataBaseHandle] openMenuDB];
        
        [self.navigationController pushViewController:myCollectVC animated:YES];
    }else if (indexPath.section == 0 && indexPath.row == 1){
        
        UIAlertView *alertView = [[UIAlertView alloc] initWithTitle:@"提示" message:@"是否清除缓存?" delegate:self cancelButtonTitle:@"取消" otherButtonTitles:@"确定", nil];
        [alertView show];
        
    }
}
// 点击alertView的方法 (签协议)
- (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex
{
    if (buttonIndex == 0) {
        
    }else if (buttonIndex == 1){
        
        // 清除菜谱缓存
        [[ClassifyDataBase sharkClassifyDataBase] openDB];
        [[ClassifyDataBase sharkClassifyDataBase] DropTable];
        
        // 清除食疗缓存
        [[FoodDataBase shareMenuDataBaseHandle] openMenuDB];
        [[FoodDataBase shareMenuDataBaseHandle] DropMenuTable];
        
        UIAlertView *alertView = [[UIAlertView alloc] initWithTitle:@"提示" message:@"清除缓存成功" delegate:self cancelButtonTitle:@"确定" otherButtonTitles: nil];
        [alertView show];
    }
}


- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
